from django.contrib import admin
from .models import InterfaceTestSuite
from .models import InterfaceList
from .models import InterfaceTestCase
from .models import SuitCase
from django.contrib.contenttypes.models import ContentType
from django.utils.html import format_html  # 将字符串格式化为html格式
from .views_interface import suitegetcase, createruncase, run_case
from django.http import HttpResponseRedirect
from django.shortcuts import render
from .forms import HostForm

admin.site.site_header = '海浪自动化测试(学习版)'
admin.site.site_title = '海浪'


# Register your models here.
@admin.register(SuitCase)
class SuitCaseAdmin(admin.ModelAdmin):
    list_display = ['suite', 'interface_id', 'case_id', 'run_order', 'interface_protocol', 'interface_host_name', 'interface_url',
                    'interface_header', 'interface_method',  'case_data', 'interface_body_form',
                    'case_expected', 'case_response', 'run_status', 'run_result', 'run_owner',
                    'run_time', 'run_creator', 'run_ctime']
    readonly_fields = ['suite', 'case_desc',  'interface_protocol',  'interface_host_name', 'interface_url',
                       'interface_header','case_data', 'interface_method', 'interface_body_form',
                       'case_expected', 'case_response', 'run_status', 'run_result', 'run_owner',
                       'run_time', 'run_creator', 'run_ctime']

    list_editable = ['run_order', 'run_status']
    # search_fields = ['interface_protocol', 'interface_host_name', 'interface_url']
    list_filter = ['suite', 'interface_id', 'case_id', 'interface_protocol', 'interface_host_name', 'interface_url',
                    'interface_method', 'run_status', 'run_result' ]


class SuiteRunInline(admin.TabularInline):
    list_display = ['run_order', 'interface_id', 'case_id', 'case_desc', 'interface_header',
                    'interface_protocol', 'interface_host_name', 'interface_url',
                    'interface_method',  'interface_body_form', 'case_data',
                    'case_expected', 'case_response', 'run_status', 'run_result', 'run_owner',
                    'run_time', 'run_creator', 'run_ctime']
    field = ('run_order', 'interface_id', 'case_id', 'case_desc', 'interface_header',
             'interface_protocol', 'interface_host_name', 'interface_url',
             'interface_method', 'interface_body_form', 'case_data',
             'case_expected', 'case_response', 'run_status', 'run_result', 'run_owner',
             'run_time')
    readonly_fields = ['interface_id', 'case_id', 'case_desc', 'interface_header',
                       'interface_protocol', 'interface_host_name', 'interface_url',
                       'interface_method', 'interface_body_form', 'case_data',
                       'case_expected', 'case_response', 'run_status', 'run_result', 'run_owner',
                       'run_time', 'run_creator', 'run_ctime']
    list_editable = ['run_order', 'run_status']
    model = SuitCase
    extra = 0  #设置为默认1个外联编辑对象


@admin.register(InterfaceTestSuite)
class SuiteAdmin(admin.ModelAdmin):
    def run_suite(self, obj):
        """
        自定义一个标签，用于存放发送按钮
        """
        suite_list = list(InterfaceTestSuite.objects.filter(id=obj.pk).values())
        dest = '/do_interface_case/{}'.format(obj.pk)
        title = '执行'
        method = suite_list[0]
        do_interface_case = format_html(
            "<a href={}  method={} name = 'do' >\
                        <span>{}</span></a>".format(dest, method['suite_name'],
                                                    title))
        return do_interface_case

    list_display = ['suite_name', 'suite_host_name', 'suite_status', 'suite_result', 'suite_modifier', 'suite_mtime', 'run_suite']
    fields = (('suite_name', 'suite_status'), 'suite_desc', 'suite_host_name', ('suite_modifier', 'suite_result'), 'suite_case')
    # list_editable = ['suite_result']
    filter_horizontal = ['suite_case']
    actions = ['do_suite', 'creat_case']
    inlines = [SuiteRunInline]

    def creat_case(self, request, queryset):
        selected = request.POST.getlist(admin.ACTION_CHECKBOX_NAME)
        for suiteid in selected:
            runcase = suitegetcase(suiteid)
            createruncase(runcase)
    creat_case.short_description = "生成用例集中的用例"
    # creat_case.icon = 'fas fa-audio-description'
    creat_case.type = 'primary'

    def do_suite(self, request, queryset):
        selected = request.POST.getlist(admin.ACTION_CHECKBOX_NAME)
        run_case(selected)
        form = HostForm(request.POST)
        print(form)
        return render(request, 'batch_update.html', {'form': form})
    do_suite.short_description = "执行用例"
    # do_suite.icon = 'far fa-dice-d6'
    do_suite.type = 'primary'


class InterfaceInline(admin.TabularInline):
    model = InterfaceTestCase
    extra = 0


@admin.register(InterfaceList)
class InterfaceAdmin(admin.ModelAdmin):
    list_display = ['product', 'interface_name', 'interface_url', 'interface_header', 'interface_protocol',
                    'interface_method', 'interface_body_form', 'interface_creator', 'interface_ctime']
    fields = ('product', 'interface_name', 'interface_desc', 'interface_header',
              ('interface_protocol', 'interface_url', 'interface_method'), 'interface_body_form',
              ('interface_modifier', 'interface_creator'))
    list_editable = ['interface_protocol', 'interface_method', 'interface_body_form']
    inlines = [InterfaceInline]


@admin.register(InterfaceTestCase)
class CaseAdmin(admin.ModelAdmin):
    list_display = ['interface', 'case_name', 'case_data', 'case_expected',
                    'case_response', 'case_creator', 'case_ctime']
    fields = ('interface', 'case_name', 'case_desc', 'case_data', ('case_expected', 'case_response'),
              ('case_modifier', 'case_creator'))
    save_as = True



